// 生产工序 - 添加 - 编辑

<template>
  <view>
    <mes-form ref="mesFormTopRef" :mesForm="mesFormTop"></mes-form>
    <mes-choose-goods top="20" type="process"></mes-choose-goods>
    <mes-goods
      :form="mesGoodsForm"
      @edit="editGoods"
      @delete="delGoods"
    ></mes-goods>
    <mes-form :mesForm="mesFormBottom"></mes-form>
    <mes-button type="2" :form="mesButtonForm"></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      options: null,
      mesFormTop: {
        row: {
          // BOM清单ID
          bom_id: '',
          // 工序编号
          purchase_code: '',
          // 工序名称
          name: '',
          // 车间ID
          workshop_id: '',
          workshop_name: '',
          // 单位工资
          wages: '',
          // 标准工时
          hours: '',
          // 商品
          info_list: ''
        },
        items: [
          {
            title: '工序编号',
            slots: {
              field: 'purchase_code',
              type: 'input'
            },
            rules: [{ type: 'required', message: '请输入工序编号' }]
          },
          {
            title: '工序名称',
            slots: {
              field: 'name',
              type: 'input'
            },
            rules: [{ type: 'required', message: '请输入工序名称' }]
          },
          {
            title: '车间',
            slots: {
              field: 'workshop_name',
              type: 'select',
              icon: 'arrow',
              select: {
                search: true,
                placeholder: '请输入关键词',
                options: [],
                searchEvent: (keyword) => {
                  this.getWorkShop(keyword)
                },
                changeEvent: (item) => {
                  this.mesFormTop.row.workshop_id = item.id
                  this.mesFormTop.row.workshop_name = item.name
                }
              }
            }
          },
          {
            title: '单位工资',
            tip: '单位工资=计时工资/标准工时',
            slots: {
              field: 'wages',
              type: 'input',
              input_type: 'digit',
              digit_type: 'price'
            }
          },
          {
            title: '标准工时',
            tip: '标准工时=计时工资/单位工资',
            slots: {
              field: 'hours',
              type: 'input',
              input_type: 'digit',
              digit_type: 'price'
            }
          }
        ]
      },
      mesFormBottom: {
        top: 20,
        row: {
          // 备注
          remark: ''
        },
        items: [
          {
            title: '备注',
            slots: {
              field: 'remark',
              type: 'input'
            }
          }
        ]
      },
      mesGoodsForm: {
        priceField: 'purchase'
      },
      mesButtonForm: {
        text: '保存',
        confirmEvent: () => {
          this.$refs.mesFormTopRef.validate(async (valid) => {
            if (!valid) return

            const goods = JSON.parse(
              JSON.stringify(getApp().globalData.goods.list)
            )
            if (!goods.length) {
              return uni.showToast({
                title: '请添加商品',
                icon: 'none'
              })
            }
            if (this.options.type === 'edit') {
              goods.map((item) => (item.goods_unit = item.unit_id))
            }

            const postForm = JSON.parse(JSON.stringify(this.mesFormTop.row))
            postForm.info_list = JSON.stringify(goods)
            postForm.remark = this.mesFormBottom.row.remark
            delete postForm.workshop_name
            const res = await this.$api.prod.procedure[this.options.type](
              postForm
            )
            if (res.code == 200) this.$tools.successBack(res.msg)
          })
        }
      }
    }
  },
  async onLoad(options) {
    this.options = options
    if (options.type === 'add') {
      // 获取编号
      const code = await this.$tools.getCode(30)
      this.mesFormTop.row.purchase_code = code || ''

      this.mesFormTop.row.bom_id = options.bom_id
    } else if (options.type === 'edit') {
      this.getDetail()
    }

    this.getWorkShop()
  },
  methods: {
    // 获取车间
    async getWorkShop(keyword) {
      const res = await this.$api.source.workshop.index({
        keyword,
        page: 1,
        status: 1
      })
      if (res.code == 200) {
        this.mesFormTop.items.forEach((item) => {
          if (item.title === '车间') {
            item.slots.select.options = res.data.list
          }
        })
      }
    },
    // 获取详情
    async getDetail() {
      const res = await this.$api.prod.procedure.getdetail({
        id: this.options.id
      })
      if (res.code == 200) {
        this.mesFormTop.row = {
          id: res.data.id,
          bom_id: res.data.bom_id,
          purchase_code: res.data.purchase_code,
          name: res.data.name,
          workshop_id: res.data.workshop_id,
          workshop_name: res.data.workshop_name,
          wages: res.data.wages,
          hours: res.data.hours,
          info_list: []
        }
        this.mesFormBottom.row = {
          remark: res.data.remark
        }
        getApp().globalData.goods.list = res.data.info_list
      }
    },
    // 编辑商品
    editGoods(index) {
      uni.navigateTo({
        url: `/pages/home/bom_list/process_goods?index=${index}`
      })
    },
    // 删除商品
    delGoods(index) {
      getApp().globalData.goods.list.splice(index, 1)
      uni.showToast({
        title: '删除成功',
        icon: 'none'
      })
    }
  }
}
</script>

<style>
page {
  padding-bottom: 200rpx;
}
</style>
